$(function () {
    getUserinfo()
/* 需求二:退出 */
    $('#logout').on('click',function(){
        layer.confirm('确认退出咩?', {icon: 3, title:'提示'}, function(index){
            //do something
            localStorage.removeItem('token')
            location.href='/login.html'
            layer.close(index);
          });
    })
})
/* let layer = layui.layer */

function getUserinfo() {
    axios({
        method: 'GET',
        url: '/my/userinfo',
        /* headers:{
            Authorization:localStorage.getItem('token')
        } */
    }).then(({
        data: res
    }) => {
        /* 判断 */
        if (res.status !== 0) {
            return layui.layer.msg(res.message)
        }
        /* 渲染 */
        renderAvatar(res.data)
    })
}
/* 渲染用户信息和头像 */
function renderAvatar(user) {
    /* 渲染欢迎如果有nickname优先渲染nickname,没有nickname渲染username */

    let name = user.nickname || user.username
    $('.welcome').html('欢迎&nbsp;' + name)
    /* 处理头像:如果有图片头像就渲染图片头像,否则渲染文字头像 */
    if (user.user_pic !== null) {
        $('.avatar-text').hide()
        $('.layui-nav-img').show().attr('src', user.user_pic)
    } else {
        let first = name[0].toUpperCase()
        $('.avatar-text').show().html(first)
        $('.layui-nav-img').hide()
    }
}